package com.elvin.loader;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import com.elvin.loader.dbhelper.DbHelper;

public class StudentContentProvider extends ContentProvider {
	
	private final String TAG = "StudentContentProvider";
	
	private final static UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
	private final static int STUDENT = 1;
	private final static int STUDENTS = 2;
	
	private DbHelper helper = null;
	
	static{
		URI_MATCHER.addURI("com.elvin.loader.StudentContentProvider", "student/#", STUDENT);
		URI_MATCHER.addURI("com.elvin.loader.StudentContentProvider", "student", STUDENTS);
	}
	
	public StudentContentProvider() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		helper = new DbHelper(getContext());
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		Cursor cursor = null;
		try {
			SQLiteDatabase database = helper.getReadableDatabase();
			int flag = URI_MATCHER.match(uri);
			switch (flag) {
			case STUDENT:
				long id = ContentUris.parseId(uri);
				String where_value = " stuid = " +id;
				if(selection != null && !selection.equals("")){
					where_value += " and "+selection;
				}
				cursor = database.query("student", null, where_value, selectionArgs, null, null, null, null);
				break;
			case STUDENTS:
				cursor = database.query("student", null, selection, selectionArgs, null, null, null);
				break;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return cursor;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		int flag = URI_MATCHER.match(uri);
		switch (flag) {
		case STUDENT:
			return "vnd.android.cursor.item/student";
		case STUDENTS:
			return "vnd.android.cursor.dir/students";
		}
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		Uri resultUri = null;
		SQLiteDatabase database = helper.getWritableDatabase();
		int flag = URI_MATCHER.match(uri);
		switch (flag) {
		case STUDENTS:
			long id = database.insert("student", null, values);
			resultUri = ContentUris.withAppendedId(uri, id);
			break;
		}
		Log.i(TAG, "-->>"+resultUri.toString());
		return resultUri;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		int count = 0;
		SQLiteDatabase database = helper.getWritableDatabase();
		int flag = URI_MATCHER.match(uri);
		switch (flag) {
		case STUDENT:
			long stuid = ContentUris.parseId(uri);
			String where_value = " stuid = "+stuid;
			if(selection != null && !selection.equals("")){
				where_value += selection;
			}
			count = database.delete("student", where_value, selectionArgs);
			break;
		case STUDENTS:
			count = database.delete("student", selection, selectionArgs);
			break;
		}
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		SQLiteDatabase database = helper.getWritableDatabase();
		int count = -1;
		try {
			//update student set name =?, address =? where id = ?
			long id = ContentUris.parseId(uri);
			int flag = URI_MATCHER.match(uri);
			switch (flag) {
			case STUDENT:
				String where_value = " stuid =  "+id;
				if (selection != null && !selection.equals("")) {
					where_value += " and "+selection;
				}
				count = database.update("student", values, where_value, selectionArgs);
				break;
			case STUDENTS:
				count = database.update("student", values, selection, selectionArgs);
				break;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return count;
	}

}
